import pygame
from pygame.locals import *
from pygame import *
import sys, os
from classes import loadimage

class button(pygame.sprite.Sprite):
    def __init__(self, name, activate, xpos, ypos, xsize, ysize, application):
        # Load image
        self.name = name
        self.activate = activate
        self.xpos = xpos
        self.ypos = ypos
        self.xsize = xsize
        self.ysize = ysize
        # Check for gif or png
        if self.activate == True:
            name = 'a' + self.name
        if os.path.isfile(application.themepath + 'buttons/button_' + name + '.png') == True:
            button = application.themepath + 'buttons/button_' + name + ".png"
        else:
            button = application.themepath + 'buttons/button_' + name + '.gif'
        if os.path.isfile(button) == True:
            image = loadimage(button, self.xsize * application.cubex, self.ysize * application.cubey)
            image.rect.x = self.xpos * application.cubex
            image.rect.y = self.ypos * application.cubey
            # Add to spritelist
            application.spritelist.append(image)
            # Render all items in spritelist
            application.spritegrouprender = pygame.sprite.OrderedUpdates(application.spritelist)
            # Draw rendered group
            application.spritegrouprender.draw(pygame.display.get_surface())
            # Show
            pygame.display.flip()

    def toggle(self, application):
        name = self.name
        if self.activate == True:
            self.activate = False
        else:
            self.activate = True
            name = 'a' + self.name
        if os.path.isfile(application.themepath + 'buttons/button_' + name + '.png') == True:
            button = application.themepath + 'buttons/button_' + name + '.png'
        else:
            button = application.themepath + 'buttons/button_' + name + '.gif'
        if os.path.isfile(button) == True:
            # Load image
            image = loadimage(button, self.xsize * application.cubex, self.ysize * application.cubey)
            image.rect.x = self.xpos * application.cubex
            image.rect.y = self.ypos * application.cubey
            # Add to spritelist => REMOVE AND THEN APPEND => LATER
            application.spritelist.append(image)
            # Render all items in spritelist
            application.spritegrouprender = pygame.sprite.OrderedUpdates(application.spritelist)
            # Draw rendered group
            application.spritegrouprender.draw(pygame.display.get_surface())
            # Show
            pygame.display.flip()
